package com.jf.cloud.service.sysmanager.support.validate;

import java.util.HashMap;
import java.util.Iterator;
import java.util.Map;

import javax.servlet.http.HttpServletRequest;

import org.apache.commons.logging.Log;
import org.apache.commons.logging.LogFactory;

public class SafetyValidateUtil{
	static Log log = LogFactory.getLog(SafetyValidateUtil.class);
	@SuppressWarnings("unchecked")
	public static boolean verify(HttpServletRequest request){
		Map<String,String> params = new HashMap<String,String>();
		Map requestParams = request.getParameterMap();
		for (Iterator iter = requestParams.keySet().iterator(); iter.hasNext();) {
			String name = (String) iter.next();
			String[] values = (String[]) requestParams.get(name);
			String valueStr = "";
			for (int i = 0; i < values.length; i++) {
				valueStr = (i == values.length - 1) ? valueStr + values[i] 
						: valueStr + values[i] + ",";
			}
			params.put(name, valueStr);
		}
		
		 if(!params.containsKey("sign")||params.get("sign").equals("")){
			 //System.out.println("==========================签名参数sign缺失或者为空:===================="+params);
			 log.info("==========================签名参数sign缺失或者为空:===================="+params);
			 return false;
		 } 
			 
		 
		    //System.out.println("==========================准备验证 参数:===================="+params);
		    log.info("==========================准备验证 参数:===================="+params);
	        String mysign = ""; // getMysign(params);
	        String sign = "";
	        if(params.get("sign") != null) {sign = params.get("sign");}

	        //System.out.println("==========================验证签名==============================");
	        //System.out.println("==========sign:"+sign+"===========mysign:"+mysign+"===============");
	        log.info("==========sign:"+sign+"===========mysign:"+mysign+"===============");
	        //System.out.println("==========================验证签名==============================");
	        if (mysign.equals(sign)) {
	            return true;
	        } else {
	            return false;
	        }
	}
	
	public static void mian(String[] args){
		Map<String, String> Params = new HashMap<String, String>();
		Params.put("sign", "c8f4f547f8a654505988af0bfbb409eb");
		Params.put("appId", "3yx046");
		Params.put("keyword", "5rWL6K V");
		Params.put("pageIndex", "1");
//		System.out.println(getMysign(Params));
		
	}
    
}
